import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * @Author: wenjingyuan
 * @Date: 2022/03/15/12:02
 * @Description:
 */
public class SelectCity {
    public static void main(String[] args) throws SQLException {
        //1.得到数据源
        MysqlDataSource dataSource=new MysqlDataSource();
        //设置URL
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?characterEncoding=utf8&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("12345678");
        //2.得到连接

        Connection connection= (Connection) dataSource.getConnection();
        //3.先组装SQL，再得到执行器

        String selSql="select * from city where id=?";
        PreparedStatement statement=connection.prepareStatement(selSql);
        statement.setInt(1,2);

        //4.执行SQL
        ResultSet result=statement.executeQuery();
        while(result.next()){
            City city=new City();
            city.setId(result.getInt("id"));
            city.setName(result.getString("name"));
            System.out.println(city);
        }
        //5.关闭资源
        result.close();
        statement.close();
        connection.close();
    }

}
